<?php

/***

LZRoster: A roster for World of Warcraft.
Copyright (C) 2007  Emmanuel Cron

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

***/

require_once dirname(__FILE__)."/File.class.php";

class FileParser
{	
	private $lua_parsed;
	private $is_valid = true;
	private $get_magic_quotes_gpc = null;
	
	function FileParser($file)
	{
		$this->lua_parsed = array();
		
		if ( count($file) == 0 )
		{
			return;
		}
		else
		{
			do
			{
				if ( strstr(current($file), "LZProfile = "))
				{
					next($file);
					return $this->recursive_parseFile ($file, $this->lua_parsed);
				}
			} while ( next($file) );
		}
	}
	
	private function recursive_parseFile(&$fArray, &$profileA)
	{
		$tindex = 0;
		
		while ( !strstr(current($fArray), "}") and key($fArray) != false )
		{
			if ( strstr(current($fArray), "= {") )
			{
				preg_match ("/\[(.+)\]/", current($fArray), $matches);
				$key = str_replace("\"", "", $matches[1]);
				$key = addslashes($key);
				$profileA[$key] = array();
				next($fArray);
				$this->recursive_parseFile($fArray, $profileA[$key]);
			}
			elseif ( strstr(current($fArray), "{") )
			{
				next($fArray);
				$tindex++;
				$this->recursive_parseFile($fArray, $profileA[$tindex]);
			}
			elseif ( strstr(current($fArray), "nil") )
			{
				$tindex++;
			}
			
			if ( preg_match("/\[(.+)\] = (.+),/", current($fArray), $matches) > 0 )
			{
				$key = str_replace("\"", "", $matches[1]);
				$key = addslashes($key);
				$value = str_replace("\"", "", $matches[2]);
				$value = addslashes($value);
				$profileA[$key] = $value;
			}
			elseif ( preg_match("/\"(.+)\", -- \[.+\]/", current($fArray), $matches) > 0 )
			{			
				$value = addslashes($matches[1]);
				$profileA[$tindex] = $value;
				
				$tindex++;
			}
			
			next($fArray);
		}
		
		if ( !strstr(current($fArray), "}") )
		{
			if ($this->is_valid == true)
			{
				$this->is_valid = false;
			}
		}
	}
	
	function IsSuccessful()
	{
		return $this->is_valid;
	}
	
	function GetParsedData()
	{
		return $this->lua_parsed;
	}
}

?>